package org.greyhawk.samonitor.domain;

import java.util.Date;

import org.greyhawk.samonitor.service.SiteCrawler;
import org.greyhawk.samonitor.service.SummaryGenerater;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

@Component
@Scope(value="prototype")
public class ImageResourcesFactory extends ResourcesFactory {
	
	@Autowired
	private SummaryGenerater summaryGenerater;
	@Autowired
	private SiteCrawler siteCrawler;
	
	@Override
	public SummaryGenerater getSummaryGenerater() {
		return summaryGenerater;
	}
	@Override
	public SiteCrawler getSiteCrawler() {
		return siteCrawler;
	}
	
	public ImageResourcesFactory(){
		super();
	}
	
	public ImageResourcesFactory(SiteCrawler siteCrawler,SummaryGenerater summaryGenerater){
		this.siteCrawler = siteCrawler;
		this.summaryGenerater = summaryGenerater;
	}

	@Override
	public Resources createResources(Site site, ResourcesType type,
			String res) throws Exception {
		Resources resources = new Resources();
		resources.setName(siteCrawler.createName(res));
		resources.setSite(site);
		resources.setSourcePath(res);
		resources.setType(type);
		byte[] data = siteCrawler.crawResources(res);
		resources.setData(data);
		String summary = null;
		summary = summaryGenerater.generateSummary(data);
		resources.setSummary(summary);
		resources.setCreateTime(new Date());
		return resources;
	}

}
